package com.example.shader;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES20;
import android.util.Log;

public class Triangle {
    public Triangle(){
        float[] quadVertexCoor = {  
                -1.0f, -1.0f, 0.0f, 1.0f, 
                1.0f, -1.0f, 0.0f, 1.0f,                                                                    
                0.0f, 1.0f, 0.0f, 1.0f, 
        };
          mQuadVerticesCoor = ByteBuffer
          .allocateDirect(quadVertexCoor.length * 4)
          .order(ByteOrder.nativeOrder()).asFloatBuffer();
          mQuadVerticesCoor.put(quadVertexCoor);
          mQuadVerticesCoor.position(0);   
    
      
          mProgram = GLES20.glCreateProgram();
          
          int vertShader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
          GLES20.glShaderSource(vertShader, vShaderByteArray);
          GLES20.glCompileShader(vertShader);
          checkGLError("vertShader");
          
          int fragShader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
          GLES20.glShaderSource(fragShader, fShaderByteArray);
          GLES20.glCompileShader(fragShader);
          checkGLError("fragShader");
          
          //为着色程序添加顶点与片元着色程序
          GLES20.glAttachShader(mProgram, vertShader);
          GLES20.glAttachShader(mProgram, fragShader);       
          checkGLError("glAttachShader");
          
          GLES20.glBindAttribLocation(mProgram, 0, "aPosition");       
          GLES20.glEnableVertexAttribArray(0);
          checkGLError("glEnableVertexAttribArray");
          
          GLES20.glLinkProgram(mProgram);
          GLES20.glDeleteShader(vertShader);
          GLES20.glDeleteShader(fragShader);      
          checkGLError("glLinkProgram"); 
    }
    
    public void draw(){
        GLES20.glUseProgram(mProgram);
        GLES20.glVertexAttribPointer(0, 4, GLES20.GL_FLOAT,
                false, 0, mQuadVerticesCoor);     
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 3);
    }
    
    public static void checkGLError(String label) {
        int error;
        while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
            Log.e(TAG, label + ": glError " + error);
            throw new RuntimeException(label + ": glError 0x" + Integer.toHexString(error));
        }
    }
    
    private static final String TAG = Triangle.class.getSimpleName();
    private int mProgram;
    private FloatBuffer mQuadVerticesCoor;  
    private final static String vShaderByteArray = new String("attribute vec4 aPosition;"+
                                                            "void main() {"+
                                                            "gl_Position = aPosition;"+
                                                            "}");
    private final static String fShaderByteArray = new String("precision mediump float;"+
                                                              "void main() {"+                                                                   
                                                              "gl_FragColor = vec4(1, 0, 0, 1);"+
                                                              "}");
}
